package com.wzq.app.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.util.Log;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import static com.jhworks.library.ui.ImageSelectorFragment.TAG;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 16/12/08
 *     desc  : Utils初始化相关
 * </pre>
 */
public class Utils {

    @SuppressLint("StaticFieldLeak")
    private static Context context;

    private Utils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 初始化工具类
     *
     * @param context 上下文
     */
    public static void init(@NonNull final Context context) {
        Utils.context = context.getApplicationContext();
    }

    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    public static Context getContext() {
        if (context != null) return context;
        throw new NullPointerException("u should init first");
    }


    /**
     * 返回当前程序版本名
     */
    public static String packageName(Context context) {
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }


    /**
     * 返回程序版本号
     *
     * @param context
     * @return
     */
    public static int packageCode(Context context) {
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    private static String mYear;
    private static String mMonth;
    private static String mDay;
    private static String mWay;

    /**
     * 获取当前年月日周几
     *
     * @return
     */
    public static String StringData() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mYear + "年" + mMonth + "月" + mDay + "日" + "/星期" + mWay;
    }

    /**
     * 获取当前年月日
     *
     * @return
     */
    public static String getDay() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        return mYear + "年" + mMonth + "月" + mDay + "日";
    }

    /**
     * 获取当前周几
     *
     * @return
     */
    public static String getDate() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "星期" + mWay;
    }

    public static String transDate(String chooseDate) {
        if (chooseDate.equals(""))
            return "";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = null;
        try {
            date = formatter.parse(chooseDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sDate = sdf.format(date);
        return sDate;
    }

    /*
     * @param imageUrl
     * @return
     */
    public static Drawable getImageFromNetwork(String imageUrl) {
        URL myFileUrl = null;
        Drawable drawable = null;
        try {
            myFileUrl = new URL(imageUrl);


            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);

            conn.connect();
            InputStream is = conn.getInputStream();
            drawable = Drawable.createFromStream(is, null);

            is.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return drawable;
    }

    /**
     * 当前预约上车的日期时间是否在有效时间内
     *
     * @param date      周几
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 是否有效
     */
    public static boolean isUse(String date, long startTime, long endTime) {
        Calendar now = Calendar.getInstance();
        now.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mWay = String.valueOf(now.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        if (date.equals("工作日")) {
            date = "星期一,星期二,星期三,星期四,星期五";
        }
//        System.out.println("星期-----" + mWay);
        if (date.contains(mWay) || date.equals("每天")) {
//            System.out.println("星期---进来啦====开始时间：" + startTime + "    结束时间：" + endTime);
            SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
            Date nowDate = null;
            try {
                nowDate = df.parse(df.format(new Date()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            Calendar nowCalendar = Calendar.getInstance();
            nowCalendar.setTime(nowDate);

//            System.out.println("---当前时间====" + nowCalendar.getTimeInMillis());
            Calendar beginCalendar = Calendar.getInstance();
            Calendar endCalendar = Calendar.getInstance();
            beginCalendar.setTimeInMillis(startTime);
            endCalendar.setTimeInMillis(endTime);

//            System.out.println("开始--" + nowCalendar.after(beginCalendar));
//            System.out.println("结束--" + nowCalendar.before(endCalendar));
//            System.out.println("相等吗--" + (beginCalendar.getTimeInMillis() == nowCalendar.getTimeInMillis()) +"----"+ (endCalendar.getTimeInMillis() == nowCalendar.getTimeInMillis()));
            if (nowCalendar.after(beginCalendar) && nowCalendar.before(endCalendar)) {
//                System.out.println("时间范围内--");
                return true;//时间范围内
            } else if (beginCalendar.getTimeInMillis() == nowCalendar.getTimeInMillis() || endCalendar.getTimeInMillis() == nowCalendar.getTimeInMillis()) {
//                System.out.println("时间范围内-  正好相等-");
                return true;//正好相等
            } else {
//                System.out.println("时间范围外--");
                return false;//范围外
            }
        }

        return false;
    }

    /**
     * 判断当前时间是否在最后营运时间后一个半小时内
     *
     * @param lastTime 最后营运时间
     * @return
     */
    public static boolean isBetAfterTime(String lastTime) {
        if (lastTime == null) {
            return false;
        }
        if (lastTime.equals("00:00")) {
            return true;
        }
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        Date dateTime = null;
        Date now = null;
        Date expireTime = null;
        try {
            now = df.parse(df.format(new Date()));
            dateTime = df.parse(lastTime);
            expireTime = new Date(dateTime.getTime() + 60 * 60 * 1000 + 30 * 60 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar date = Calendar.getInstance();
        date.setTime(now);
        Calendar expire = Calendar.getInstance();
        expire.setTime(expireTime);

        if (expire.after(date)) {
            System.out.println("时间范围内-----");
            return true;//时间范围内
        } else if (expireTime.getTime() == now.getTime()) {
            System.out.println("正好相等-----");
            return true;//正好相等
        } else {
            System.out.println("范围外-----");
            return false;//范围外
        }
    }
}